package com.horay.statemachiedemo.single;

import com.horay.statemachiedemo.single.enums.OrderState;
import com.horay.statemachiedemo.single.event.OrderEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.statemachine.StateMachine;

/**
 * Class description
 *
 * @author heyingcheng
 * @date 2020/10/23
 */
@Slf4j
@SpringBootApplication
public class StatemachieSingleApplication implements CommandLineRunner {

    @Autowired
    private StateMachine<OrderState, OrderEvent> singleMachine;

	public static void main(String[] args) {
		SpringApplication.run(StatemachieSingleApplication.class, args);
	}

    @Override
    public void run(String... args) throws Exception {
        // 创建流程
        singleMachine.start();
        // 触发PAY事件
        singleMachine.sendEvent(OrderEvent.PAY);
        // 触发RECEIVE事件
        singleMachine.sendEvent(OrderEvent.RECEIVE);
        // 获取最终状态
        log.info("最终状态:[{}]", singleMachine.getState().getId());
    }

}
